package com.dianrui.qiyouriding.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.dianrui.qiyouriding.Constant;
import com.dianrui.qiyouriding.R;
import com.dianrui.qiyouriding.adapter.HelpCateListAdapter;
import com.dianrui.qiyouriding.bean.HelpList;
import com.dianrui.qiyouriding.okhttp.OkHttpRequest;
import com.dianrui.qiyouriding.util.StringUtils;
import com.dianrui.qiyouriding.util.ToastUtil;
import com.dianrui.qiyouriding.util.Url;
import com.dianrui.qiyouriding.view.PulltoRefresh.PullToRefreshBase;
import com.dianrui.qiyouriding.view.PulltoRefresh.PullToRefreshListView;
import com.google.gson.JsonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class HelpCateListActivity extends BaseActivity {
    private HelpCateListAdapter helpListAdapter;
    private List<HelpList> cateHelpListAll = new ArrayList<>();
    private  int mPageIndex=1;
    private String mCateIds;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.help_cate_listview)
    PullToRefreshListView  helpList;
    @BindView(R.id.emptyView)
    LinearLayout emptyView;

    @Override
    public int getContentViewId() {
        return R.layout.help_cate_list;
    }

    @Override
    public void initView() {
        customInit(true,R.color.main_color);
        if (getIntent() != null && getIntent().getExtras()!=null) {
            Bundle bundle=getIntent().getExtras();
            if (bundle != null) {
                title.setText(bundle.getString("title"));
                mCateIds=bundle.getString("cate_id");
            }

        }
        getHelpList(false);
        helpListAdapter = new HelpCateListAdapter(HelpCateListActivity.this, cateHelpListAll);
        helpList.setAdapter(helpListAdapter);
        helpList.setMode(PullToRefreshBase.Mode.BOTH);
        helpList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                getHelpList(true);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                mPageIndex++;
                getHelpList(false);

            }
        });
    }

    private void getHelpList(final boolean isF) {
        if (isF) {
            mPageIndex = 1;
        }
        JsonObject jsonObject = new JsonObject();
        if (!StringUtils.isEmpty(SPUtils.getInstance().getString("member_id"))) {
            jsonObject.addProperty("member_id", SPUtils.getInstance().getString("member_id"));
        }
        jsonObject.addProperty("cate_id", mCateIds);
        jsonObject.addProperty("p", mPageIndex);
        OkHttpRequest.getInstance().postRequests(Url.HELP_LIST, jsonObject.toString(),  new OkHttpRequest.XCallBack() {

            @Override
            public void onResponse(String result) {
                helpList.onRefreshComplete();
                if (isF) {
                    cateHelpListAll.clear();
                }
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject &&!isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                JSONArray jsonArray = jsonObject.optJSONArray("data");
                                if (jsonArray.length() > 0) {
                                    for (int i = 0; i < jsonArray.length(); i++) {
                                        HelpList helpList = new HelpList();
                                        helpList.article_id = jsonArray.optJSONObject(i).optString("article_id");
                                        helpList.title = jsonArray.optJSONObject(i).optString("title");
                                        cateHelpListAll.add(helpList);
                                    }
                                    if (helpListAdapter != null) {
                                        helpListAdapter.refresh(cateHelpListAll);
                                    }

                                } else {
                                    ToastUtil.showToast(getString(R.string.no_data));
                                }

                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }else {
                    ToastUtil.showToast(getString(R.string.net_error));
                }
                if (cateHelpListAll.size() <= 0) {
                    emptyView.setVisibility(View.VISIBLE);
                    helpList.setVisibility(View.GONE);
                } else {
                    emptyView.setVisibility(View.GONE);
                    helpList.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void failedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }

    @Override
    public void initData() {

    }


    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }
}
